package com.example.luke.cashorderingsystem.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.example.luke.cashorderingsystem.Model.Good;
import com.example.luke.cashorderingsystem.R;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by luke on 08/12/2016.
 */

public class GoodCustomAdapter extends BaseExpandableListAdapter {
    private ShoppingCartListener shoppingCartListener;
    private  ArrayList<Integer>purchaseCountList;
    private ExpandableListView listView;

    class ViewHolder{
        Button addButton;
        Button minButton;
        TextView goodName;
        TextView totalPrice;
        TextView priceOut;
        TextView currentPurchaseCountTxt;
        TextView storedNumber;
        Good respGood;
        Button delButton;

    }
     public interface ShoppingCartListener{
        void reloadPrice();
        void goBack();
        void calcVal(HashMap<String,Object> hashMap,int butId);
        void delItem(String viewPosition);



    }

    public void setShoppingCartListener(ShoppingCartListener shoppingCartListener) {
        this.shoppingCartListener = shoppingCartListener;
    }

    private LayoutInflater inflater;
    private Context context;
    private ArrayList<Good>shoppingList;

    public GoodCustomAdapter(Context context){
        this.context=context;
        inflater=LayoutInflater.from(context);

    }

    public GoodCustomAdapter(ExpandableListView listView,Context context, ArrayList<Good>shoppingList){
        this.listView=listView;
        this.context=context;
        this.shoppingList=shoppingList;
        inflater=LayoutInflater.from(context);
        purchaseCountList=new ArrayList<>();

    }
    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public Good getChild(int groupPosition, int childPosition) {
        return shoppingList.get(groupPosition);
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    private  Good good;
    private Integer currentPurchaseCount;
    private TextView currentPurchaseCountTxt;
    private TextView total_price;
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
       ViewHolder holder=null;


        if (convertView==null){

            convertView=inflater.inflate(R.layout.shopping_cart_custom_row,null);
            holder=new ViewHolder();

            holder.addButton=(Button)convertView.findViewById(R.id.min);


            holder.minButton=(Button)convertView.findViewById(R.id.add);


            holder.totalPrice=(TextView)convertView.findViewById(R.id.total_price);
            holder.storedNumber=(TextView)convertView.findViewById(R.id.storedNumber);
            holder.priceOut=(TextView)convertView.findViewById(R.id.priceOut);
            holder.currentPurchaseCountTxt=(TextView)convertView.findViewById(R.id.purchaseCount);


            convertView.setTag(holder);


        }else {
            holder=(ViewHolder) convertView.getTag();



        }

        int position=groupPosition;

        HashMap<String,Object> temp=new HashMap<>();
        temp.put("position",position+"");
        temp.put("view",convertView);

        holder.addButton.setTag(temp);
        holder.minButton.setTag(temp);


        holder.respGood=getGroup(position);
        good=getChild(position,childPosition);



        currentPurchaseCount=good.getPurchaseCount();
        purchaseCountList.add(currentPurchaseCount);

//        holder.goodName.setText(good.getName());
        holder.priceOut.setText(good.getPriceOut()+"元");
        holder.storedNumber.setText("x"+good.getStoredNumber());


        currentPurchaseCountTxt=holder.currentPurchaseCountTxt;
        currentPurchaseCountTxt.setText(String.valueOf(currentPurchaseCount));


        currentPurchaseCount=good.getPurchaseCount();

        total_price=holder.totalPrice;

        total_price.setText(good.getTotalPrice()+"元");


        holder.minButton.setOnClickListener(calcPurchaseCount());
        holder.addButton.setOnClickListener(calcPurchaseCount());



        return convertView;

    }

    private View.OnClickListener calcPurchaseCount(){

        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if (shoppingCartListener!=null){

                    shoppingCartListener.calcVal((HashMap) v.getTag(),v.getId());

                }

                //  reloadCorrespondView();
            }
        };

    }



    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    @Override
    public Good getGroup(int groupPosition) {
        return shoppingList.get(groupPosition);

    }

    @Override
    public int getGroupCount() {
        return shoppingList.size();

    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder holder=null;

        if (convertView==null){
            convertView=inflater.inflate(R.layout.good_header_layout,null);
            holder=new ViewHolder();
            holder.goodName=(TextView)convertView.findViewById(R.id.goodName);

            holder.delButton=(Button)convertView.findViewById(R.id.delGood);
            convertView.setTag(holder);
        }
       // convertView.setPadding(0,20,0,20);

     //  listView.setDividerHeight(0);
        TextView itemDetail=(TextView)convertView.findViewById(R.id.itemDetail);
        Good respGood=shoppingList.get(groupPosition);
        String temp="x"+respGood.getPurchaseCount()+" ("+respGood.getTotalPrice()+"元)";
        itemDetail.setText(temp);



        holder=(ViewHolder)convertView.getTag();
        holder.goodName.setText(getGroup(groupPosition).getName());
        holder.delButton.setId(groupPosition);
        holder.delButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (shoppingCartListener!=null)
                    shoppingCartListener.delItem(v.getId()+"");
            }
        });


        return convertView;

    }
}
